<?php

namespace App\Model;

/**
 * Video manager class
 */
class VideoManager extends \Nette\Object {

  private $em;
  private $videoDao;

  /**
   * Init object
   * @param \Kdyby\Doctrine\EntityManager $em
   */
  public function __construct(\Kdyby\Doctrine\EntityManager $em) {
    $this->em = $em;
    $this->videoDao = $em->getDao(Dao\Video::getClassName());
  }

  /**
   * Video by id
   * @param int $id
   * @return Video
   */
  public function getVideoById($id) {
    $video = $this->videoDao->find($id);
    return $video;
  }

  /**
   * Video by parameter webalize
   * @param string $webalize
   * @return Video
   */
  public function getVideoByWebalize($webalize) {
    $video = $this->videoDao->findOneBy(array('webalize' => $webalize));
    return $video;
  }

  /**
   * Views count
   * @param Video $video
   * @return int
   */
  public function getVideoViewsCount($video) {
    $view = $this->em->getDao(Dao\VideoView::getClassName());
    return $view->countBy(array('video' => $video));
  }

  /**
   * Video comments count
   * @param Video $video
   * @return int
   */
  public function getVideoCommentsCount($video) {
    $view = $this->em->getDao(Dao\VideoComment::getClassName());

    return $view->countBy(array('video' => $video));
  }

  /**
   * Find related videos
   * @param int $learn
   * @param int $limit
   * @return array
   */
  public function getRelatedVideosArray($learn, $limit) {
    $qb = $this->em->createQueryBuilder();

    $qb->select('v.id, v.category, v.title, v.webalize, v.duration, v.learn , v.style, v.level');
    $qb->addSelect('(SELECT COUNT(vc.id)
            FROM ' . Dao\VideoComment::getClassName() . ' vc
            WHERE vc.video = v) AS comments');
    $qb->addSelect('(SELECT COUNT(vv.id)
            FROM ' . Dao\VideoView::getClassName() . ' vv
            WHERE vv.video = v) AS views');
    $qb->from(Dao\Video::getClassName(), ' v');
    $qb->where('v.learn >' . $learn);
    $qb->orderBy('v.learn');
    $qb->setMaxResults($limit);

    return $qb->getQuery()->getArrayResult();
  }

  /**
   * Related videos for last video
   * @param int $learn
   * @param int $limit
   * @return array
   */
  public function getRelatedVideosArrayDesc($learn, $limit) {
    $qb = $this->em->createQueryBuilder();

    $qb->select('v.id, v.category, v.title, v.webalize, v.duration, v.learn , v.style, v.level');
    $qb->addSelect('(SELECT COUNT(vc.id)
            FROM ' . Dao\VideoComment::getClassName() . ' vc
            WHERE vc.video = v) AS comments');
    $qb->addSelect('(SELECT COUNT(vv.id)
            FROM ' . Dao\VideoView::getClassName() . ' vv
            WHERE vv.video = v) AS views');
    $qb->from(Dao\Video::getClassName(), ' v');
    $qb->where('v.learn <' . $learn);
    $qb->orderBy('v.learn', 'DESC');
    $qb->setMaxResults($limit);

    return $qb->getQuery()->getArrayResult();
  }

  /**
   * Get id', 'video_id', 'category', 'title', 
   * 'webalize_title', 'free', 'duration', 'learn_line', 'views', 'style', 'level
   * @return array
   */
  public function getAllVideosBaseArray() {

    $qb = $this->em->createQueryBuilder();

    $qb->select('v.id, v.category, v.title, v.webalize, v.duration, v.learn , v.style, v.level');
    $qb->addSelect('(SELECT COUNT(vc.id)
            FROM ' . Dao\VideoComment::getClassName() . ' vc
            WHERE vc.video = v) AS comments');
    $qb->addSelect('(SELECT COUNT(vv.id)
            FROM ' . Dao\VideoView::getClassName() . ' vv
            WHERE vv.video = v) AS views');
    $qb->from(Dao\Video::getClassName(), ' v');

    return $qb->getQuery()->getArrayResult();
  }

}
